#include "opedb.h"
#include <QMessageBox>
#include <QDebug>
#include <QTextEdit>
#include <QString>
OpeDB::OpeDB(QObject *parent) : QObject(parent)
{
    m_db = QSqlDatabase::addDatabase("QSQLITE");//告诉qt是什么数据库
}

OpeDB &OpeDB::getInstance()
{
    static OpeDB instance;
    return instance;
}

void OpeDB::init()//初始化
{
    m_db.setHostName("localhost");//链接本地数据库
    m_db.setDatabaseName("D:\\qt_widgets\\TcpServer1\\cloud.db");//不能跟其他资源一样直接添加进去，需要找到数据库文件路径
    if(m_db.open())
    {
        QSqlQuery query;
        query.exec("select * from userinfo");
        while(query.next())
        {
            QString data = QString("%1,%2,%3").arg(query.value(0).toString()).arg(query.value(1).toString()).arg(query.value(2).toString());
            qDebug() << data;
        }
    }
    else
    {
        QMessageBox::critical(nullptr,"打开数据库","打开数据库失败");
    }

}

OpeDB::~OpeDB()
{
    m_db.close();//析构函数关闭数据库
}

bool OpeDB::handleRegist(const char *name, const char *pwd)
{
    if(name == nullptr || pwd == nullptr) return false; //未传入任何信息则返回false，必须进行形参有效性的判断！
    QString data = QString("insert into userInfo(name, pwd) values (\'%1'\,\'%2\')").arg(name).arg(pwd);//名字替换%1，密码替换%2  插入信息，姓名和密码，类型是char*所以注意values格式
    qDebug() << data;
    QSqlQuery query;
    return query.exec(data);
}

bool OpeDB::handleLogin(const char *name, const char *pwd)//注意登陆成功要把online的值改为1
{
    if(name == nullptr || pwd == nullptr) return false; //未传入任何信息则返回false,必须进行形参有效性的判断！
    QString data = QString("select * from userInfo where name = \'%1\' and pwd = \'%2\' and online = 0").arg(name).arg(pwd);//online = 0表示未登录，等于1表示已经登录了
    qDebug() << data;
    QSqlQuery query;
    query.exec(data);
    if(query.next())//里面有数据，就是向着下一个迭代，next获得数据返回true，没获得的返回false
    {
        data = QString("update userInfo set online=1 where name = \'%1\' and pwd = \'%2\'").arg(name).arg(pwd);//online = 0表示未登录，等于1表示已经登录了
        qDebug() << data;
        QSqlQuery query;
        query.exec(data);
        return true;
    }
    else
    {
        return false;
    }
}

void OpeDB::handleOffline(const char *name)//数据库设置下线。
{
    if(name == nullptr) return;
    QString data = QString("update userInfo set online=0 where name=\'%1\'").arg(name);//online = 0表示未登录，等于1表示已经登录了
    //qDebug() << data;
    QSqlQuery query;
    query.exec(data);
}

QStringList OpeDB::handleAllOnline()//查询在线的用户
{
    QString data = QString("select name from userInfo where online = 1");//online = 0表示未登录，等于1表示已经登录了

    QSqlQuery query;//查询语句
    query.exec(data);
    QStringList result;
    result.clear();
    //通过next获得结果集
    while(query.next())
    {
        result.append(query.value(0).toString()); // 添加名字进去,value(0)是name
    }
    return result;
}

int OpeDB::handleSearchUser(const char *name)//调用这个函数就知道是否存在这个用户
{
    if(name == nullptr) return -1;//不存在
    QString data = QString("select online from userInfo where name=\'%1\'").arg(name);
    QSqlQuery query;
    query.exec(data);
    if(query.next())//下一位存在状态
    {
//        int ret =  query.value(0).toInt();
//        if(ret == 1) return 1;
//        else if(ret == 0) return 0;
        return query.value(0).toInt();
    }
    else return -1;//不存在
}



int OpeDB::handleAddFriend(const char *pername, const char *name)
{
    if(pername == nullptr || name == nullptr)//有效性判断
    {
        return -1;
    }
    QString data = QString("select * from friend where (id =(select id from userInfo where name = \'%1\') and friendId = (select id from userInfo where name=\'%2\')) or (id=(select id from userInfo where name = \'%3\') and friendId = (select id from userInfo where name=\'%4\'))").arg(pername).arg(name).arg(name).arg(pername);
    qDebug() <<data;
    QSqlQuery query;
    query.exec(data);
    if(query.next())
        return 0;//双方已经是好友
    else
    {   
        data = QString("select online from userInfo where name=\'%1\'").arg(pername);
        QSqlQuery query;
        query.exec(data);
        if(query.next())//下一位存在状态
        {
            int ret =  query.value(0).toInt();
            if(ret == 1) return 1;//在线
            else if(ret == 0) return 2;//不在线
            else return 3;//不存在
        }
        else return -1;//不存在
    }
        

}

QStringList OpeDB::handleFlushFriend(const char *name)
{
    QStringList strFriendList;
    strFriendList.clear();
    if(name == nullptr) return strFriendList;
    QString data = QString("select name from userInfo where online = 1 and id in (select id from friend where friendId = (select id from userInfo where name = \'%1\'))").arg(name);
    QSqlQuery query;
    query.exec(data);
    while(query.next())
    {
        strFriendList.append(query.value(0).toString());//放入字符串列表
        qDebug() << query.value(0).toString();
    }
    query.clear();
    data = QString("select name from userInfo where online = 1 and id in (select friendId from friend where id = (select id from userInfo where name = \'%1\'))").arg(name);
    query.exec(data);
    while(query.next())
    {
        strFriendList.append(query.value(0).toString());//放入字符串列表
        qDebug() << query.value(0).toString();
    }

    return strFriendList;
}

bool OpeDB::handleDelFriend(const char *name, const char *friendName)
{
    if(name == nullptr || friendName == nullptr) return false;
    QString data = QString("delete from friend where id = (select id from userInfo where name = \'%1\') and friendId = (select id from userInfo where name =\'%2\' )").arg(name).arg(friendName);
    QSqlQuery query;
    query.exec(data);

    data = QString("delete from friend where id = (select id from userInfo where name = \'%1\') and friendId = (select id from userInfo where name =\'%2\' )").arg(friendName).arg(name);

    query.exec(data);
    return true;
}
